﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using WebApplication3.Common;
using WebApplication3.Models;
using System.Text.Json;
using System.Collections;
using Newtonsoft.Json.Linq;

namespace WebApplication3.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private IMemoryCache _cache;

        public HomeController(ILogger<HomeController> logger, IMemoryCache memoryCache)
        {
            _logger = logger;
            _cache = memoryCache;
        }

        public class User
        {
            public int stopId { get; set; }
            public int routeId { get; set; }
        }

        public IActionResult Index()
        {
            string jsonString1 = "{ \"routeList\":[ { \"stopId\":35,\"routeId\":23} ]}";
            JObject jsonObject = JObject.Parse(jsonString1);
            string routeList = jsonObject["routeList"].ToString();
            var test23 = JsonConvert.DeserializeObject<List<User>>(routeList);

            double num12 = -56646.236234;
            double num13 = Math.Round(num12, 2);
            double num14 = Math.Abs(num13);

            List<User> users = new List<User>
        {
            new User { stopId = 101,routeId=1 },

            new User { stopId = 102 ,routeId=1},
            new User { stopId = 101,routeId=2 },
            new User { stopId = 103 ,routeId=3},
            new User { stopId = 101,routeId=1 }
        };
            //var str1 = str.GroupBy(i => i).Where(g => g.Count() > 1)
            List<IGrouping<int, User>> count = users.GroupBy(a => a.stopId).Where(a => a.Count() > 1).ToList();
            foreach (var item in count)
            {
                var name12 = item.Key;
            }

            var text = users.OrderByDescending(a => a.stopId).ToList();
            int index = users.FindIndex(a => a.stopId == 101);

            int route = users.Where(a => a.stopId == 200)?.FirstOrDefault()?.routeId ?? 120;
            int[] _stopId = users.Select(a => a.stopId).ToArray();
            int aa = _stopId.Length;
            int bb = _stopId.Count();

            ArrayList arrayList = new ArrayList();
            arrayList.Add(12);
            arrayList.Add(13);
            arrayList.Add(14);
            for (int i = 0; i < arrayList.Count; i++)
            {
                int s = Convert.ToInt32(arrayList[i]);
            }
            string[] name13 = new string[] { "张三", "张三", "张三12" };
            //_cache.Set<>();

            int a = 44444;
            short b = 0;
            Int16.TryParse(a.ToString(), out b);

            Random random = new Random();
            int c = random.Next(0, 100);
            //System.Text.Json.JsonSerializer.Serialize("");

            int[] d = new int[] { 1, 3, 5, 7 };
            d = d.Skip(1).ToArray();

            List<RouteMapRDV> list_line = new List<RouteMapRDV>();
            List<RouteMapStop> list_station = new List<RouteMapStop>();
            RouteMapRDV lines = new RouteMapRDV();
            lines.RouteMapRDVID = 1;
            lines.displayName = "K521";
            lines.routeColor = "5d9532";
            lines.routePath = "";
            RouteMapStop station = new RouteMapStop();
            station.RouteMapStopID = 01;
            station.stopX = 405;
            station.stopY = 86;
            station.stopName = "北安河";
            station.stopChiName = "Bac Yen";
            station.virtualStop = false;
            station.nameTop = 26;
            station.nameRight = 0;
            list_station.Add(station);
            lines.routeMapStops = list_station;
            list_line.Add(lines);

            List<RouteMapStop> list_station1 = new List<RouteMapStop>();
            RouteMapRDV lines1 = new RouteMapRDV();
            lines.RouteMapRDVID = 2;
            lines.displayName = "K522";
            lines.routeColor = "07c4c2";
            lines.routePath = "";
            RouteMapStop station1 = new RouteMapStop();
            station1.RouteMapStopID = 02;
            station1.stopX = 675;
            station1.stopY = 846;
            station1.stopName = "安河桥北";
            station1.stopChiName = "Anheqiaobei station";
            station1.virtualStop = false;
            station1.nameTop = 26;
            station1.nameRight = 0;
            list_station1.Add(station1);
            lines1.routeMapStops = list_station1;
            list_line.Add(lines1);

            string jsonString = JsonConvert.SerializeObject(list_line);

            string data = HttpUtil.GetUrlHtml(url: "http://www.baidu.com");

            string name = "1|2|3";

            return View();
        }


        /// <summary>
        ///请求路线信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetRouteCollect()
        {
            string name = "{\"routeCode\": \"506_TMFP_TMS\",\"displayName\": \"506\",\"routeColor\": \"5d9532\",\"routePath\":\"\",\"RouteMapStop\": [ {\"id\": 1177,\"stopId\": 102,\"stopCode\": \"506-U010\",\"stopChiName\": \"Tuen Mun Ferry Pier\",\"stopX\": \"405\",\"stopY\": \"86\",\"stopName\": \"Tuen Mun\",\"stopOnRouteStatisticalId\": 1849,\"routeId\": 65,\"seq\": 1,\"distanceOnRoute\": 0.0,\"distanceFromPreviousStop\": 0.0,\"geometry\": null    }]}";
            RouteMapRDV lines2 = JsonConvert.DeserializeObject<RouteMapRDV>(name);
            string jsonString = JsonConvert.SerializeObject(lines2);
            return Json(jsonString);
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }

        public IActionResult Test()
        {
            return View();
        }

        public IActionResult Test1()
        {
            return View();
        }
    }
}
